package com.isoft.c2team3service2.controller;

import com.isoft.c2team3service2.bean.ResponseData;
import com.isoft.c2team3service2.dto.ArtistDTO;
import com.isoft.c2team3service2.dto.FollowUserDTO;
import com.isoft.c2team3service2.entity.UserDTO;
import com.isoft.c2team3service2.service.FollowService;
import com.isoft.c2team3service2.service.impl.BusinessException;
import com.isoft.c2team3service2.util.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/follow")
public class FollowController extends BaseController {

    @Autowired
    private FollowService followService;

    /**
     * 关注用户
     */
    @PostMapping("/user/{followedUserId}")
    public ResponseData<Void> followUser(@PathVariable Integer followedUserId) {
        if (UserHolder.getUser() == null) {
            return result(null, "请先登录");
        }

        try {
            followService.followUser(followedUserId);
            return result(null, "关注成功");
        } catch (BusinessException e) {
            return result(null, e.getMessage());
        }
    }

    /**
     * 取消关注用户
     */
    @DeleteMapping("/user/{followedUserId}")
    public ResponseData<Void> unfollowUser(@PathVariable Integer followedUserId) {
        if (UserHolder.getUser() == null) {
            return result(null, "请先登录");
        }

        try {
            followService.unfollowUser(followedUserId);
            return result(null, "取消关注成功");
        } catch (BusinessException e) {
            return result(null, e.getMessage());
        }
    }

    /**
     * 关注艺人
     */
    @PostMapping("/artist/{artistId}")
    public ResponseData<Void> followArtist(@PathVariable Integer artistId) {
        if (UserHolder.getUser() == null) {
            return result(null, "请先登录");
        }

        try {
            followService.followArtist(artistId);
            return result(null, "关注成功");
        } catch (BusinessException e) {
            return result(null, e.getMessage());
        }
    }

    /**
     * 取消关注艺人
     */
    @DeleteMapping("/artist/{artistId}")
    public ResponseData<Void> unfollowArtist(@PathVariable Integer artistId) {
        if (UserHolder.getUser() == null) {
            return result(null, "请先登录");
        }

        try {
            followService.unfollowArtist(artistId);
            return result(null, "取消关注成功");
        } catch (BusinessException e) {
            return result(null, e.getMessage());
        }
    }

    /**
     * 获取当前用户关注的所有用户
     */
    @GetMapping("/users")
    public ResponseData<List<FollowUserDTO>> getFollowedUsers() {
        if (UserHolder.getUser() == null) {
            return result(null, "请先登录");
        }

        List<FollowUserDTO> followedUsers = followService.getFollowedUsers();
        return result(followedUsers, "查询成功");
    }

    /**
     * 获取当前用户关注的所有艺人
     */
    @GetMapping("/artists")
    public ResponseData<List<ArtistDTO>> getFollowedArtists() {
        if (UserHolder.getUser() == null) {
            return result(null, "请先登录");
        }

        try {
            List<ArtistDTO> followedArtists = followService.getFollowedArtists();
            return result(followedArtists, "查询成功");
        } catch (BusinessException e) {
            return result(null, e.getMessage());
        }
    }

    // 辅助方法，简化ResponseData创建
    private <T> ResponseData<T> result(T data, String message) {
        ResponseData<T> response = new ResponseData<>();
        response.setData(data);
        response.setMsg(message);
        response.setErrorCode(data != null || "成功".equals(message) ? 0 : 1);
        return response;
    }
}
